package db;

import Info.SystemEnum;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.User;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ManageUserDB {
    private List<User> list;
    private Log log;
    public ManageUserDB(Log log) {
        list = DataBase.getInstance().getUsersList();
        this.log = log;
    }

    public ManageUserDB(List<User> list) {
        this.list = list;
        log = LogFactory.getLog(getClass());
    }

    public boolean add(User user){
        if(user!=null){
            if(find(user)==-1){
                list.add(user);
                log.info(getClass()+"\tadd\t"+SystemEnum.SUCCESS.toString());
                return true;
            }else{
                log.info(SystemEnum.ERROR_REPEAT.toString());
                return false;
            }
        }
        log.info(SystemEnum.ERROR_MATCH.toString());
        return false;
    }


    //查找用户
    public int find(User find){
        int index = 0;
        if(find!=null){
            for(User f : list){
                if(f.equals(find)) break;
                index+=1;
            }
            if(index<list.size()){
                log.info(getClass()+"\tfind\t"+SystemEnum.SUCCESS.toString());
                return index;
            }
        }
        log.info(SystemEnum.ERROR_MATCH.toString());
        return -1;
    }
    //查找字段
    public List<User> find(String field,String content){
        List<User> findByField = new ArrayList<>();
        if(content==null) return findByField;
        try {
            switch (field){
                case "username":
                    for(User find : list){
                        if(find.getUsername().equals(content)) findByField.add(find);
                    }
                    break;
                case "password":
                    for(User find : list){
                        if(find.getPassword().equals(content)) findByField.add(find);
                    }
                    break;
                case "power":
                    for(User find : list){
                        if(find.getPower()==Integer.valueOf(content)) findByField.add(find);
                    }
                    break;
                case "balance<":
                    for(User find : list){
                        if(find.getBalance().doubleValue()<Double.valueOf(content)) findByField.add(find);
                    }
                    break;
                case "balance=":
                    for(User find : list){
                        if(find.getBalance().doubleValue()==Double.valueOf(content)) findByField.add(find);
                    }
                    break;
                case "balance>":
                    for(User find : list){
                        if(find.getBalance().doubleValue()>Double.valueOf(content)) findByField.add(find);
                    }
                    break;
                default:
                    log.info(SystemEnum.ERROR_INPUT.toString());
            }
        }catch (Exception e){
            log.info(SystemEnum.ERROR_INPUT.toString());
            return null;
        }
        return findByField;
    }
    public boolean delete(User user){
        if(user!=null){
            int index = find(user);
            if(index == -1){
                log.info(SystemEnum.ERROR_MATCH.toString());
                return false;
            }else{
                list.remove(index);
                log.info(getClass()+"\tdelete\t"+SystemEnum.SUCCESS.toString());
                return true;
            }
        }else{
            log.info(SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }
    public boolean modify(User user,String field,String content){
        if(user!=null){
            int index = find(user);
            if(index==-1){
                log.info(SystemEnum.ERROR_MATCH.toString());
                return false;
            }else{
                User find = list.get(index);
                try {
                    switch (field) {
                        case "username":
                            find.setUsername(content);
                            break;
                        case "password":
                            find.setPassword(content);
                            break;
                        case "power":
                            find.setPower(Integer.valueOf(content));
                            break;
                        case "balance":
                            find.setBalance(new BigDecimal(Double.valueOf(content)));
                            break;
                        default:
                            log.info(SystemEnum.ERROR_INPUT.toString());
                    }
                    log.info(getClass()+"\tmodify\t"+SystemEnum.SUCCESS.toString());
                    return true;
                }catch (Exception e){
                    log.info(SystemEnum.ERROR_INPUT.toString());
                    return false;
                }
            }
        }else{
            log.info(SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }

    public void printAll(){
        System.out.println(list);
    }
}
